﻿Public Class frmUser
    Dim dbBase As New dbUser
    Dim frmCtrl As New FormControl
    Dim dsBase As DataSet
    Dim Var As New varDeclare
    Dim TypeAct As String

    Private Sub tsNew_Click(ByVal sender As Object) Handles ToolbarHeader1.tsNewClick
        TypeAct = "I"
        Textbox(True, True)
    End Sub

    Private Sub tsEdit_Click(ByVal sender As Object) Handles ToolbarHeader1.tsEditClick
        TypeAct = "U"
        Textbox(True, False)
    End Sub

    Private Sub tsDelete_Click(ByVal sender As Object) Handles ToolbarHeader1.tsDeleteClick
        If (txtUserID.Text.Trim() <> "") Then
            TypeAct = "D"
            Dim result As String = dbBase.UserEntry(TypeAct, txtUserID.Text.Trim(), txtUsername.Text.Trim(), txtPassword1.Text.Trim(), MDI.LoginID, Var.CompID)
            If (result = "Success") Then
                frmCtrl.openChild(New frmUser, Me.ParentForm)
            Else : MessageBox.Show(result)
            End If
        Else : MessageBox.Show("Please Choose User First")
        End If
    End Sub

    Private Sub tsPrint_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsPrintClick
        ''Event 
    End Sub

    Private Sub tsSave_Click(ByVal sender As Object) Handles ToolbarHeader1.tsSaveClick
        Dim status As Boolean = True
        If (txtPassword1.Text.Trim() <> txtPassword2.Text.Trim()) Then
            MessageBox.Show("Password is not match")
            status = False
        End If
        If (TypeAct = "I" And txtPassword1.Text.Trim() = "") Then
            MessageBox.Show("Password is Empty")
            status = False
        End If
        If (txtUserID.Text.Trim() <> "" And txtUsername.Text.Trim() <> "" And status = True) Then
            Dim result As String = dbBase.UserEntry(TypeAct, txtUserID.Text.Trim(), txtUsername.Text.Trim(), txtPassword1.Text.Trim(), MDI.LoginID, Var.CompID)
            If (result = "Success") Then
                frmCtrl.openChild(New frmUser, Me.ParentForm)
            Else : MessageBox.Show(result)
            End If
        Else : MessageBox.Show("Please Fill All Field")
        End If
    End Sub

    Private Sub tsRefresh_Click(ByVal sender As Object) Handles ToolbarHeader1.tsRefreshClick
        frmCtrl.openChild(New frmUser, Me.ParentForm)
    End Sub

    Private Sub tsCancel_Click(ByVal sender As Object) Handles ToolbarHeader1.tsCancelClick
        frmCtrl.openChild(New frmUser, Me.ParentForm)
    End Sub

    Private Sub GridCellEnter_Click(ByVal sender As GridAndFind, KeyValue As String) Handles GridAndFind1.GridCellEnter
        Dim dvFind As DataView = New DataView(dsBase.Tables(0))
        dvFind.RowFilter = "[User ID] = '" & KeyValue & "'"
        txtUsername.Text = dvFind.Item(0).Row("User Name").ToString()
        txtUserID.Text = dvFind.Item(0).Row("User ID").ToString()
        txtPassword1.Text = ""
        txtPassword2.Text = ""
    End Sub
    Private Sub frmUser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dsBase = dbBase.UserList(Var.CompID)
        Textbox(False, False)
        GridAndFind1.Initialize(dsBase, "User ID", ToolbarHeader1, Me, True)
        ToolbarHeader1.Iniliaze("Form : User Management", Me, Nothing, GridAndFind1.getDataGridView(), "User ID")
        If (dsBase IsNot Nothing) Then
            GridAndFind1.getDataGridView().Columns("User Password").Visible = False
            GridAndFind1.getDataGridView().Columns("User Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        End If
    End Sub

    Private Sub Textbox(st As Boolean, clear As Boolean)
        If (st) Then
            st = False
        Else : st = True
        End If
        txtUserID.ReadOnly = st
        txtUserID.BackColor = Color.White
        txtPassword1.ReadOnly = st
        txtPassword1.BackColor = Color.White
        txtPassword2.ReadOnly = st
        txtPassword2.BackColor = Color.White
        GridAndFind1.Enabled = st
        If (clear) Then
            txtUserID.Text = ""
            txtUsername.Text = ""
            txtPassword1.Text = ""
            txtPassword2.Text = ""
        End If
    End Sub
End Class